package org.huel.backend.controller;

import org.huel.backend.domain.entity.AttendanceSummary;
import org.huel.backend.domain.entity.CourseAttendance;
import org.huel.backend.service.impl.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/dashboard")
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    /**
     * 获取仪表盘总览数据
     * @return 仪表盘数据
     */
    @GetMapping("/summary")
    public ResponseEntity<Map<String, Object>> getDashboardSummary() {
        Map<String, Object> response = new HashMap<>();

        // 获取总体出勤摘要
        AttendanceSummary summary = dashboardService.getAttendanceSummary();

        // 获取课程出勤情况
        List<CourseAttendance> courseAttendance = dashboardService.getAllCourseAttendance();

        response.put("summary", summary);
        response.put("courseAttendance", courseAttendance);
        response.put("lastUpdated", new Date());

        return ResponseEntity.ok(response);
    }

    /**
     * 获取出勤率最高的课程
     * @param limit 数量限制
     * @return 课程列表
     */
    @GetMapping("/top-present")
    public ResponseEntity<List<CourseAttendance>> getTopPresentCourses(
            @RequestParam(defaultValue = "5") int limit) {
        List<CourseAttendance> courses = dashboardService.getTopPresentCourses(limit);
        return ResponseEntity.ok(courses);
    }

    /**
     * 获取缺勤率最高的课程
     * @param limit 数量限制
     * @return 课程列表
     */
    @GetMapping("/top-absent")
    public ResponseEntity<List<CourseAttendance>> getTopAbsentCourses(
            @RequestParam(defaultValue = "5") int limit) {
        List<CourseAttendance> courses = dashboardService.getTopAbsentCourses(limit);
        return ResponseEntity.ok(courses);
    }
}